package com.xiangwushuo.framework.activity.test.test

import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.lib.common.base.mvvm.CommonViewModelActivity
import com.lib.framework.entity.Event
import com.lib.framework.manager.LogManager
import com.xiangwushuo.framework.R
import com.xiangwushuo.framework.databinding.ActivityTestBinding

/**
 * Copyright (C), 2019-2020
 * FileName: TestActivity
 * Author:
 * Date: 2020-01-01 00:00
 * Description: 测试 页面
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class TestActivity : CommonViewModelActivity<TestViewModel>() {

    companion object {

        /**
         * 发给 activity 的事件
         */
        const val EVENT_XXX = 1 // XXX 事件
    }

    private var mDataBinding: ActivityTestBinding? = null

    override fun getViewModelClass() = TestViewModel::class.java

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_test)
        mDataBinding?.viewModel = mViewModel

        initView()
    }

    /**
     * 初始化视图
     */
    private fun initView() {
        testApi()
    }

    override fun dealEvent(event: Event?): Boolean {
        // 处理 viewModel 发送过来的事件
        when (event?.code) {
            EVENT_XXX -> {
                val extraObj = event.extraObj
                if (extraObj is String) {
                    LogManager.i(mTag, "XXX 事件 的额外参数 extraObj = $extraObj")
                }
                return true
            }
        }

        return super.dealEvent(event)
    }

    /**
     * 测试网络请求
     */
    private fun testApi() {
        mDataBinding?.let { dataBinding ->
//            dataBinding.btnTv.setOnClickListener {
//                val url =
//                    " https://api.seniverse.com/v3/weather/now.json?key=SZzh6hTKTCzQoUNej&location=beijing&language=zh-Hans&unit=c"
//                ApiHelper.getService(ApiService::class.java)?.getData02(url)
//                    ?.request(object : ComRequestCallback<GetData02Resp>() {
//                        override fun onSuccess02(data: GetData02Resp?) {
//                            val s = data?.toString()
//                            LogManager.e("keanbin", "onResponse s = $s")
//                            dataBinding.text.text = "onResponse s = $s"
//                        }
//
//                        override fun onFailure(t: Throwable) {
//                            LogManager.e("keanbin", "getData", t)
//                            dataBinding.text.text = t.toString()
//                        }
//                    })
//            }
        }
    }
}
